package com.haier.cre.commonframe.weight.loading;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.ColorInt;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.haier.cre.commonframe.R;

import pl.droidsonroids.gif.GifImageView;

/**
 * 项目名：CommonLibrary
 * 包 名：com.haier.cre.commonframe.weight.loading
 * 创建人：18002063
 * 日 期：2018/10/16
 * 简 述：加载Dialog
 */
public class LoadingDialog extends Dialog {

    public static final int HORIZONTAL = 0;
    public static final int VERTICAL = 1;
    private static LoadingDialog dialog;
    private Context context;
    private TextView loadingMessage;
    private GifImageView gifMovieView;
    private LinearLayout loadingView;
    private ColorDrawable drawable;

    public LoadingDialog(Context context) {
        super(context, R.style.loading_dialog);
        this.context = context;
        drawable = new ColorDrawable();
        setContentView(R.layout.loading_dialog);
        loadingMessage = (TextView) findViewById(R.id.frame_loading_message);
        gifMovieView = (GifImageView) findViewById(R.id.frame_loading_gif);
        gifMovieView.setImageResource(R.drawable.icon_loading);
        loadingView = (LinearLayout) findViewById(R.id.frame_loading_view);
        loadingMessage.setPadding(0, 15, 0, 0);
        drawable.setColor(Color.TRANSPARENT);
        OutdatedUtils.setBackground(loadingView, drawable);
    }

    public static LoadingDialog with(Context context) {
        if (dialog == null) {
            dialog = new LoadingDialog(context);
        }
        return dialog;
    }

    public LoadingDialog setOrientation(int orientation) {
        loadingView.setOrientation(orientation);
        if (orientation == HORIZONTAL) {
            loadingMessage.setPadding(15, 0, 0, 0);
        } else {
            loadingMessage.setPadding(0, 15, 0, 0);
        }
        return dialog;
    }

    public LoadingDialog setBackgroundColor(@ColorInt int color) {
        drawable.setColor(color);
        OutdatedUtils.setBackground(loadingView, drawable);
        return dialog;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (dialog != null)
            dialog = null;
    }

    public LoadingDialog setCanceled(boolean cancel) {
        setCanceledOnTouchOutside(cancel);
        setCancelable(cancel);
        return dialog;
    }

    public LoadingDialog setMessage(String message) {
        if (!StringUtils.isSpace(message)) {
            loadingMessage.setText(message);
        }
        return this;
    }

    public LoadingDialog setMessageColor(@ColorInt int color) {
        loadingMessage.setTextColor(color);
        return this;
    }
}
